package com.hjq.controller;

import com.hjq.common.Result;
import com.hjq.exception.ServiceException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
/**
 * 文件上传
 */
@RestController
@RequestMapping
public class UploadController {
    /**
     * 上传文件
     * @param file  文件
     * @return 文件上传后的路径集合
     */
    @PostMapping("/upload")
    public Result<List<String>> upload(MultipartFile file, String dir) {
        // 文件上传后的路径集合
        ArrayList<String> arrayList = new ArrayList<>();
        if(file.isEmpty())
            throw new ServiceException(400,"上传文件不能为空");
        String newFilename= getNewFilename(Objects.requireNonNull(file.getOriginalFilename()));
        if(dir==null || dir.isEmpty())
            dir="dir";
        String day=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
        String dataPath="E:\\Code\\JAVA\\TL\\TianLiang-Cloud-Class\\TianLiang-Cloud-Class-Backend\\src\\main\\resources\\data";
        String subDataPath=dir+"/"+day;
        String filePath=dataPath+"/"+
                subDataPath+"/"+
                newFilename;
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new ServiceException(500,"文件上传失败");
        }
        arrayList.add(subDataPath+ "/"+ newFilename);
        return Result.success(arrayList);
    }
    /**
     * 上传多个文件
     * @param files  文件
     * @return 文件上传后的路径集合
     */
    @PostMapping("/uploads")
    public Result<List<String>> uploads(MultipartFile[] files,String dir) {
        // 文件路径集合
        ArrayList<String> arrayList = new ArrayList<>();
        if(files.length==0)
            throw new ServiceException(400,"上传文件不能为空");
        for (MultipartFile file : files) {
            String newFilename= getNewFilename(Objects.requireNonNull(file.getOriginalFilename()));
            if(dir.isEmpty())
                dir="dir";
            String day=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
            String dataPath="E:/Code/JAVA/TL/TL_SSM_Project/03-SpringMVC/src/main/resources/data";
            String subDataPath=dir+"/"+day;
            String filePath=dataPath+"/"+
                    subDataPath+"/"+
                    newFilename;
            try {
                file.transferTo(new File(filePath));
            } catch (IOException e) {
                throw new ServiceException(500,"文件上传失败");
            }
            arrayList.add(subDataPath+ "/"+ newFilename);
        }
        return Result.success(arrayList);
    }
    /**
     * 获取新的文件名
     * @param filename 文件名
     * @return 新的文件名
     * 新文件名由 文件名+UUID+文件后缀组成
     */
    public static String getNewFilename(String filename){
        String suffix= filename.substring(filename.lastIndexOf("."));
        return filename.substring(0,filename.lastIndexOf(suffix))
                + UUID.randomUUID()+suffix;
    }

    public static void main(String[] args) {
        System.out.println(getNewFilename("123.jpg"));
    }
}
